#include "StdAfx.h"
#include "deadZ80.h"
#include "opcodes.h"

int CDeadZ80::ExecuteOpcodeDD()
{
	unsigned char opcode = ReadMemory8(pc++);
	unsigned short stmp,tmp16;
	unsigned char tmp;
	unsigned long ltmp;

	switch(opcode) {
		case 0x09:	ADD16(IX,BC);				cycles += 7;	break;
		case 0x19:	ADD16(IX,DE);				cycles += 7;	break;

		case 0x21:	//ld IX,nn
			IX = ReadMemory16(pc);
			pc += 2;
			cycles += 14;
			break;
		case 0x22:	//ld (nn),IX
			WriteMemory16(ReadMemory16(pc),IX);
			pc += 2;
			cycles += 14;
			break;
		case 0x23:	//inc IX
			IX++;
			cycles += 10;
			break;
		case 0x24:	//inc ixh
			IX = ((IX + 0x100) & 0xFF00) | (IX & 0xFF);
			cycles += 10;
			break;
		case 0x25:	//dec ixh
			IX = ((IX - 0x100) & 0xFF00) | (IX & 0xFF);
			cycles += 10;
			break;

		case 0x26:	//ld ixh,n
			IX = (ReadMemory8(pc++) << 8) | (IX & 0xFF);
			cycles += 10;
			break;
		case 0x29:	ADD16(IX,IX);	cycles += 6;	break;
		case 0x39:	ADD16(IX,sp);	cycles += 7;	break;

		case 0x2A:	//ld IX,(nn)
			IX = ReadMemory16(ReadMemory16(pc));
			pc += 2;
			cycles += 14;
			break;
		case 0x2B:	//dec IX
			IX--;
			cycles += 10;
			break;
		case 0x2C:	//inc ixl
			IX = ((IX & 0xFF00) | ((IX + 1) & 0xFF));
			IX += 0x100;
			cycles += 10;
			break;
		case 0x2D:	//dec ixl
			IX = ((IX & 0xFF00) | ((IX - 1) & 0xFF));
			cycles += 10;
			break;
		case 0x2E:	//ld ixl,n
			IX = ReadMemory8(pc++) | (IX & 0xFF00);
			cycles += 10;
			break;

		case 0x34:	//inc (IX+d)
			ltmp = (unsigned long)(unsigned short)((signed short)IX + (signed char)ReadMemory8(pc++));
			tmp = ReadMemory8(ltmp);
			INC(tmp);
			WriteMemory8(ltmp,tmp);
			cycles += 19;
			break;

		case 0x35:	//dec (IX+d)
			ltmp = (unsigned long)(unsigned short)((signed short)IX + (signed char)ReadMemory8(pc++));
			tmp = ReadMemory8(ltmp);
			DEC(tmp);
			WriteMemory8(ltmp,tmp);
			cycles += 19;
			break;

		case 0x36:	//ld (IX+d),n
			ltmp = (unsigned long)(unsigned short)((signed short)IX + (signed char)ReadMemory8(pc++));
			WriteMemory8(ltmp,ReadMemory8(pc++));
			cycles += 19;
			break;

		case 0x40:	B = B;								cycles += 4;	break;
		case 0x41:	B = C;								cycles += 4;	break;
		case 0x42:	B = D;								cycles += 4;	break;
		case 0x43:	B = E;								cycles += 4;	break;
		case 0x44:	B = IXH;							cycles += 4;	break;
		case 0x45:	B = IXL;							cycles += 4;	break;
		case 0x46:	//ld b,(IX+d)
			ltmp = (unsigned long)(unsigned short)((signed short)IX + (signed char)ReadMemory8(pc++));
			B = ReadMemory8(ltmp);
			cycles += 19;
			break;
		case 0x47:	B = A;								cycles += 4;	break;
		case 0x48:	C = B;								cycles += 4;	break;
		case 0x49:	C = C;								cycles += 4;	break;
		case 0x4A:	C = D;								cycles += 4;	break;
		case 0x4B:	C = E;								cycles += 4;	break;
		case 0x4C:	C = IXH;							cycles += 4;	break;
		case 0x4D:	C = IXL;							cycles += 4;	break;
		case 0x4E:	//ld c,(IX+d)
			ltmp = (unsigned long)(unsigned short)((signed short)IX + (signed char)ReadMemory8(pc++));
			C = ReadMemory8(ltmp);
			cycles += 19;
			break;
		case 0x4F:	C = A;								cycles += 4;	break;
		case 0x50:	D = B;								cycles += 4;	break;
		case 0x51:	D = C;								cycles += 4;	break;
		case 0x52:	D = D;								cycles += 4;	break;
		case 0x53:	D = E;								cycles += 4;	break;
		case 0x54:	D = IXH;							cycles += 4;	break;
		case 0x55:	D = IXL;							cycles += 4;	break;
		case 0x56:	//ld d,(IX+d)
			ltmp = (unsigned long)(unsigned short)((signed short)IX + (signed char)ReadMemory8(pc++));
			D = ReadMemory8(ltmp);
			cycles += 19;
			break;
		case 0x57:	D = A;								cycles += 4;	break;
		case 0x58:	E = B;								cycles += 4;	break;
		case 0x59:	E = C;								cycles += 4;	break;
		case 0x5A:	E = D;								cycles += 4;	break;
		case 0x5B:	E = E;								cycles += 4;	break;
		case 0x5C:	E = IXH;							cycles += 4;	break;
		case 0x5D:	E = IXL;							cycles += 4;	break;
		case 0x5E:	//ld e,(IX+d)
			ltmp = (unsigned long)(unsigned short)((signed short)IX + (signed char)ReadMemory8(pc++));
			E = ReadMemory8(ltmp);
			cycles += 19;
			break;
		case 0x5F:	E = A;								cycles += 4;	break;
		case 0x60:	H = B;								cycles += 4;	break;
		case 0x61:	H = C;								cycles += 4;	break;
		case 0x62:	H = D;								cycles += 4;	break;
		case 0x63:	H = E;								cycles += 4;	break;
		case 0x64:	H = IXH;							cycles += 4;	break;
		case 0x65:	H = IXL;							cycles += 4;	break;
		case 0x66:	//ld h,(IX+d)
			ltmp = (unsigned long)(unsigned short)((signed short)IX + (signed char)ReadMemory8(pc++));
			H = ReadMemory8(ltmp);
			cycles += 19;
			break;
		case 0x67:	H = A;								cycles += 4;	break;
		case 0x68:	L = B;								cycles += 4;	break;
		case 0x69:	L = C;								cycles += 4;	break;
		case 0x6A:	L = D;								cycles += 4;	break;
		case 0x6B:	L = E;								cycles += 4;	break;
		case 0x6C:	L = IXH;							cycles += 4;	break;
		case 0x6D:	L = IXL;							cycles += 4;	break;
		case 0x6E:	//ld l,(IX+d)
			ltmp = (unsigned long)(unsigned short)((signed short)IX + (signed char)ReadMemory8(pc++));
			L = ReadMemory8(ltmp);
			cycles += 19;
			break;
		case 0x6F:	L = A;								cycles += 4;	break;

		case 0x70:	//ld (IX+d),b
			ltmp = (unsigned long)(unsigned short)((signed short)IX + (signed char)ReadMemory8(pc++));
			WriteMemory8(ltmp,B);
			cycles += 19;
			break;
		case 0x71:	//ld (IX+d),c
			ltmp = (unsigned long)(unsigned short)((signed short)IX + (signed char)ReadMemory8(pc++));
			WriteMemory8(ltmp,C);
			cycles += 19;
			break;
		case 0x72:	//ld (IX+d),d
			ltmp = (unsigned long)(unsigned short)((signed short)IX + (signed char)ReadMemory8(pc++));
			WriteMemory8(ltmp,D);
			cycles += 19;
			break;
		case 0x73:	//ld (IX+d),e
			ltmp = (unsigned long)(unsigned short)((signed short)IX + (signed char)ReadMemory8(pc++));
			WriteMemory8(ltmp,E);
			cycles += 19;
			break;
		case 0x74:	//ld (IX+d),h
			ltmp = (unsigned long)(unsigned short)((signed short)IX + (signed char)ReadMemory8(pc++));
			WriteMemory8(ltmp,H);
			cycles += 19;
			break;
		case 0x75:	//ld (IX+d),l
			ltmp = (unsigned long)(unsigned short)((signed short)IX + (signed char)ReadMemory8(pc++));
			WriteMemory8(ltmp,L);
			cycles += 19;
			break;

		case 0x77:	//ld (IX+d),a
			ltmp = (unsigned long)(unsigned short)((signed short)IX + (signed char)ReadMemory8(pc++));
			WriteMemory8(ltmp,A);
			cycles += 19;
			break;
		case 0x78:	A = B;								cycles += 4;	break;
		case 0x79:	A = C;								cycles += 4;	break;
		case 0x7A:	A = D;								cycles += 4;	break;
		case 0x7B:	A = E;								cycles += 4;	break;
		case 0x7C:	A = IXH;							cycles += 4;	break;
		case 0x7D:	A = IXL;							cycles += 4;	break;
		case 0x7E:	//ld a,(IX+d)
			ltmp = (unsigned long)(unsigned short)((signed short)IX + (signed char)ReadMemory8(pc++));
			A = ReadMemory8(ltmp);
			cycles += 19;
			break;
		case 0x7F:	A = A;								cycles += 4;	break;
		case 0x84:	ADD(IXH);							cycles += 9;	break;
		case 0x85:	ADD(IXL);							cycles += 9;	break;
		case 0x86:	//add a,(IX+d)
			ltmp = (unsigned long)(unsigned short)((signed short)IX + (signed char)ReadMemory8(pc++));
			ADD(ReadMemory8(ltmp));
			cycles += 19;
			break;
		case 0x8C:	ADC(IXH);							cycles += 9;	break;
		case 0x8D:	ADC(IXL);							cycles += 9;	break;
		case 0x8E:	//adc a,(IX+d)
			ltmp = (unsigned long)(unsigned short)((signed short)IX + (signed char)ReadMemory8(pc++));
			ADC(ReadMemory8(ltmp));
			cycles += 19;
			break;
		case 0x94:	SUB(IXH);							cycles += 9;	break;
		case 0x95:	SUB(IXL);							cycles += 9;	break;
		case 0x96:	//sub a,(IX+d)
			ltmp = (unsigned long)(unsigned short)((signed short)IX + (signed char)ReadMemory8(pc++));
			SUB(ReadMemory8(ltmp));
			cycles += 19;
			break;
		case 0x9C:	SBC(IXH);							cycles += 9;	break;
		case 0x9D:	SBC(IXL);							cycles += 9;	break;
		case 0x9E:	//sbc a,(IX+d)
			ltmp = (unsigned long)(unsigned short)((signed short)IX + (signed char)ReadMemory8(pc++));
			SBC(ReadMemory8(ltmp));
			cycles += 19;
			break;
		case 0xA4:	AND(IXH);							cycles += 9;	break;
		case 0xA5:	AND(IXL);							cycles += 9;	break;
		case 0xA6:
			ltmp = (unsigned long)(unsigned short)((signed short)IX + (signed char)ReadMemory8(pc++));
			tmp = ReadMemory8(ltmp);
			AND(tmp);
			cycles += 14;
			break;
		case 0xAC:	XOR(IXH);							cycles += 9;	break;
		case 0xAD:	XOR(IXL);							cycles += 9;	break;
		case 0xAE:
			ltmp = (unsigned long)(unsigned short)((signed short)IX + (signed char)ReadMemory8(pc++));
			tmp = ReadMemory8(ltmp);
			XOR(tmp);
			cycles += 14;
			break;
		case 0xB4:	OR(IXH);							cycles += 9;	break;
		case 0xB5:	OR(IXL);							cycles += 9;	break;
		case 0xB6:
			ltmp = (unsigned long)(unsigned short)((signed short)IX + (signed char)ReadMemory8(pc++));
			tmp = ReadMemory8(ltmp);
			OR(tmp);
			cycles += 14;
			break;
		case 0xBC:	CP(IXH);							cycles += 9;	break;
		case 0xBD:	CP(IXL);							cycles += 9;	break;
		case 0xBE:
			ltmp = (unsigned long)(unsigned short)((signed short)IX + (signed char)ReadMemory8(pc++));
			tmp = ReadMemory8(ltmp);
			CP(tmp);
			cycles += 14;
			break;
		case 0xCB:	return(ExecuteOpcodeDDCB());
		case 0xE1:	//pop IX
			POP16(IX);
			break;
		case 0xE5:	//push IX
			PUSH16(IX);
			cycles += 11;
			break;
		default:
			message("bad DD opcode $%02X\n",opcode);
			return(1);
	}
	return(0);
}
